package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.QuestAnswerListData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.http.HttpUtil.MyResponse;
import com.wbao.dianniu.listener.IKnowAnswerAdd;
import com.wbao.dianniu.listener.IKnowAnswerAddListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yejianjun
 */
public class KnowAnswerAddManger implements IKnowAnswerAdd {

    private List<IKnowAnswerAddListener> mListener = new ArrayList<IKnowAnswerAddListener>();
    private Context mContext;
    public static KnowAnswerAddManger instance;

    public static KnowAnswerAddManger getInstance(Context ctx) {
        if (null == instance) {
            instance = new KnowAnswerAddManger(ctx);
        }
        return instance;
    }

    private KnowAnswerAddManger(Context context) {
        mContext = context;
    }

    @Override
    public void knowAnswerAdd(int accountId, int knowledgeId, String content, String noticeJson, String answerImgs, int pid, int anonymous) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACCOUNT_KNOWLEDGE_ANSWER_ADD)
                    .addArgs("accountId", accountId)
                    .addArgs("knowledgeId", knowledgeId)
                    .addArgs("content", content)
                    .addArgs("noticeJson", noticeJson)
                    .addArgs("answerImgs", answerImgs)
                    .addArgs("pid", pid)
                    .addArgs("anonymous", anonymous);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                QuestAnswerListData lit = JsonUtil.toObject(obj, QuestAnswerListData.class);
                if (null != mListener) {
                    for (int i = 0; i < mListener.size(); i++) {
                        mListener.get(i).onKnowAnswerAddSuccess(lit);
                    }
                }
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener) {
                    for (int i = 0; i < mListener.size(); i++) {
                        mListener.get(i).onKnowAnswerAddFailure(errorCode, msg);
                    }
                }
            }
        });
    }

    @Override
    public boolean addknowAnswerAddListener(IKnowAnswerAddListener listener) {
        if (null != mListener) {
            mListener.add(listener);
        }
        return mListener != null;
    }

    @Override
    public boolean removeknowAnswerAddListener(IKnowAnswerAddListener listener) {
        if (null != mListener) {
            mListener.remove(listener);
        }
        return true;
    }

}
